package graphicinterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Book;
import bookworm.BooksAndRatingOfOneAuthor;

/**
 * this class will be responsible for drawing books from one specific author to the user.
 * There's a filter on the window that can let the user view just the books from the tag
 * specified by him (when he wanted to search for best authors based on tag) or all the
 * books from the author.
 * @author Mina
 *
 */
public class WindowBooksFromAuthor implements MouseListener
{
	private LinkedList<Book> allBooksFromAuthor;
	private LinkedList<Book> booksFromAuthorRelatedToTag;
	private String author;
	private String tag;
	
	private JFrame windowBooksFromAuthor;
	private JLabel labelWithTitleOfWindowBooksFromAuthor; /*this label will change 
										depending of the filter used to view the books*/
	
	/*the 2 filters below will change the currently showing books to books from just 
	the tag or all the books from the author*/
	private JLabel filterAllBooks;
	private JLabel filterBooksFromTag;
	private String currentFilter;
	
	private JPanel fatherOfPanelsWithBooks; 
	/*the "father" panel above will contain one JPanel of class ShowsJPanelsOnManyPages inside it.
	When we want to change the filter(books from specific tag to all books or vice-versa), this
	panel will remove everything that's inside of him (which means a "ShowsJPanelsOnManyPages",
	a panel with lots of book panels inside of it) and substitute with something else*/
	
	private LinkedList<JPanel> panelsWithBooksFromSpecificTag;
	private LinkedList<JPanel> panelsWithAllBooks;
	
	public WindowBooksFromAuthor(BooksAndRatingOfOneAuthor booksAndRatingOfOneAuthor)
	{
		this.allBooksFromAuthor = booksAndRatingOfOneAuthor.getAllBooksFromAuthor();
		this.booksFromAuthorRelatedToTag = 
				booksAndRatingOfOneAuthor.getBooksFromAuthorRelatedToTag();
		this.author = booksAndRatingOfOneAuthor.getAuthor();
		this.tag = booksAndRatingOfOneAuthor.getTag();
		this.windowBooksFromAuthor = new JFrameWithMenu("books from specific author " + author);
		
		this.panelsWithBooksFromSpecificTag = this.createPanelsFromAllBooksFromSpecifiedTag();
		this.panelsWithAllBooks = this.createPanelsFromAllBooks();
		drawWindowBooksFromAuthor();
	}
	
	private void drawWindowBooksFromAuthor()
	{
		JPanelWithBackgroundImage panelBooksFromAuthor =
				new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		
		//first, let's add the panel with the title of this section of our software
		this.labelWithTitleOfWindowBooksFromAuthor = 
				TextLabelCreator.createStyledTextlabel(tag + " books from author " + author, 25);
		
		JPanel panelWithTitleOfWindowBooksFromAuthor = new JPanel();
		panelWithTitleOfWindowBooksFromAuthor.setBackground(new Color(255,131,137));
		panelWithTitleOfWindowBooksFromAuthor.add(labelWithTitleOfWindowBooksFromAuthor);
		
		GridBagLayout layout = new GridBagLayout();
		panelBooksFromAuthor.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridheight = 1;
		panelBooksFromAuthor.add(panelWithTitleOfWindowBooksFromAuthor, grid);
		
		//now let's create the panel with filter options
		
		grid.gridy = 1;
		JPanel panelWithFilterOptions = this.createPanelWithFilterOptions();
		panelBooksFromAuthor.add(panelWithFilterOptions, grid);
		
		
		//now let's add the books panels on the "father" and let him be on "panelBooksFromAuthor"
		ShowsJPanelsOnManyPages containPanelsWithBooks =
				new ShowsJPanelsOnManyPages(panelsWithBooksFromSpecificTag, 3, 1);
		containPanelsWithBooks.setBackground(new Color(255,131,137));
		
		this.fatherOfPanelsWithBooks = new JPanel();
		fatherOfPanelsWithBooks.setBackground(new Color(255,131,137));
		fatherOfPanelsWithBooks.add(containPanelsWithBooks);
		
		grid.gridheight = 3;
		grid.gridy = 2;
		panelBooksFromAuthor.add(fatherOfPanelsWithBooks, grid);
		
		this.windowBooksFromAuthor.add(panelBooksFromAuthor);
		this.windowBooksFromAuthor.setVisible(true);
	}
	
	
	/**
	 * creates JPanels with book cover, character and rating(on stars) for all books
	 * written by author and from tag that the rating is based on. This tag was informed
	 * by the user when he informed that he wanted to search for best authors based
	 * on tag
	 * @return
	 */
	private LinkedList<JPanel> createPanelsFromAllBooksFromSpecifiedTag()
	{
		LinkedList<JPanel> panelsFromAllBooksFromSpecifiedTag = new LinkedList<JPanel>();
		for(int i = 0; i < booksFromAuthorRelatedToTag.size(); i++)
		{
			Book oneBook = booksFromAuthorRelatedToTag.get(i);
			JPanelWithBookStarsAndCharacter panelOfOneBook = 
					new JPanelWithBookStarsAndCharacter(oneBook);
			panelOfOneBook.addMouseListener(this);
			panelsFromAllBooksFromSpecifiedTag.add(panelOfOneBook);
		}
		return panelsFromAllBooksFromSpecifiedTag;
	}
	

	/**
	 * creates JPanels with book cover, character and rating(on stars) for all books
	 * written by author(no specific tag).
	 * @return
	 */
	private LinkedList<JPanel> createPanelsFromAllBooks()
	{
		LinkedList<JPanel> panelsFromAllBooks = new LinkedList<JPanel>();
		for(int i = 0; i < allBooksFromAuthor.size(); i++)
		{
			Book oneBook = allBooksFromAuthor.get(i);
			JPanelWithBookStarsAndCharacter panelOfOneBook =
					new JPanelWithBookStarsAndCharacter(oneBook);
			panelOfOneBook.addMouseListener(this);
			panelsFromAllBooks.add(panelOfOneBook);
		}
		return panelsFromAllBooks;
	}
	
	/**
	 * creates a panel with the two JLabels that will do the filter on the books viewed
	 * by the user on this screen
	 * @return
	 */
	private JPanel createPanelWithFilterOptions()
	{
		JPanel filterOptions = new JPanel();
		filterOptions.setBackground(new Color(255,131,137));
		JLabel labelFilterBy = TextLabelCreator.createStyledTextlabel("filter by:");
		this.filterBooksFromTag = TextLabelCreator.createStyledTextlabel("books from specific tag");
		this.filterBooksFromTag.setForeground(Color.blue);
		this.filterAllBooks = TextLabelCreator.createStyledTextlabel("all books");
		this.filterAllBooks.setForeground(Color.red);
		
		JPanel panelFilterBy = new JPanel();
		panelFilterBy.add(labelFilterBy);
		panelFilterBy.setBackground(new Color(255,131,137));
		
		JPanel panelFilterBooksFromTag = new JPanel();
		panelFilterBooksFromTag.add(this.filterBooksFromTag);
		panelFilterBooksFromTag.setBackground(new Color(255,131,137));
		
		JPanel panelFilterBooksAllBooks = new JPanel();
		panelFilterBooksAllBooks.add(this.filterAllBooks);
		panelFilterBooksAllBooks.setBackground(new Color(255,131,137));
		
		GridBagLayout layout = new GridBagLayout();
		filterOptions.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		grid.insets = new Insets(0,0,0,10);
		filterOptions.add(panelFilterBy, grid);
		
		grid.insets = new Insets(0,0,0,20);
		filterOptions.add(panelFilterBooksFromTag, grid);
		filterOptions.add(panelFilterBooksAllBooks, grid);
		
		this.filterAllBooks.addMouseListener(this);
		this.filterBooksFromTag.addMouseListener(this);
		this.currentFilter = "books from specific tag";
		return filterOptions;
	}
	
	private void filterChanged(String whichFilterShouldBeUsed)
	{
		this.fatherOfPanelsWithBooks.removeAll();
		ShowsJPanelsOnManyPages containPanelsWithBooks;
		
		if(whichFilterShouldBeUsed.compareTo("all books") == 0)
		{
			containPanelsWithBooks =
					new ShowsJPanelsOnManyPages(panelsWithAllBooks, 3, 1);
			containPanelsWithBooks.setBackground(new Color(255,131,137));
			labelWithTitleOfWindowBooksFromAuthor.setText("all books from author " + this.author);
		}
		else
		{
			//filter is "books from specific tag"
			containPanelsWithBooks =
					new ShowsJPanelsOnManyPages(panelsWithBooksFromSpecificTag, 3, 1);
			containPanelsWithBooks.setBackground(new Color(255,131,137));
			labelWithTitleOfWindowBooksFromAuthor.setText(tag + 
														" books from author " + this.author);
		}
		
		/*we'll have to give back the focus to "containPanelsWithBooks" (or else the user
		  couldn't go to other pages with books from author), that's why
		  we'll minimize the window and then maximize it again
		*/
		containPanelsWithBooks.requestFocus();
		this.windowBooksFromAuthor.setState(JFrame.ICONIFIED);
		this.windowBooksFromAuthor.setState(JFrame.NORMAL);
		this.fatherOfPanelsWithBooks.add(containPanelsWithBooks);
		this.fatherOfPanelsWithBooks.updateUI();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		Component objectClicked = arg0.getComponent();
		if(objectClicked == this.filterAllBooks)
		{
			this.currentFilter = "all books";
		}
		else if(objectClicked == this.filterBooksFromTag)
		{
			this.currentFilter = "books from specific tag";
		}
		else if(objectClicked instanceof JPanelWithBookStarsAndCharacter == true)
		{
			//it the user clicked on a book, this window should be disposed
			this.windowBooksFromAuthor.dispose();
		}
		this.filterChanged(this.currentFilter);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) 
	{
		Component objectEntered = arg0.getComponent();
		if(objectEntered == this.filterAllBooks || objectEntered == this.filterBooksFromTag)
		{
			/*the mouse icon will turn into the "object can be selected" form
			  (that little hand with pointy finger)*/
			objectEntered.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
